<?

/** Import export library
 *  @author Jyry Kuukkanen
 */

include_once("soset.php");
include_once("sodebug.php");
include_once("sodebug.inc");
include_once("dzkone.php");
include_once("soinifiles.php");

/* End of file string */
define(DZIMPEXP_EOF, chr(12));

/** Import function
 *  This function imports a Dataxi import file or a folderful of them and feeds
 *  them to specified database.
 *  @param string $IniStream Input stream string for ini data
 *  @param string $FolderName Import folder name
 *  @package dzimpexp
 */
function dzImport($IniStream, $FolderName = "") {
    include("sodebug.inc");
    #$soDebugLevel = SOD_DET;
    #$soDebugLogFile = "/tmp/luteet.log";

    /* Get initial values from ini file */
    $ini = new soIniData($IniStream);
    $dbname = $ini->getValue("general", "database");
    $lockfile = $ini->getValue("general", "lockfile");

    if ($FolderName == "") {
        $source = $ini->getValue("file", "source");
    } else {
        $source = $FolderName;
    };
    $postfix = $ini->getValue("file", "extension");
    $linefeed = urldecode($ini->getValue("file", "linefeed"));

    $failed_log = $ini->getValue("file", "failed_log");
    $error_log = $ini->getValue("log", "error");
    $audit_log = $ini->getValue("log", "audit");
    $echo = $ini->getValue("log", "audit", "bool");

    /* check for lockfile and exit if it exists */
    if (file_exists($lockfile)) die(); // <-- EXIT
    exec("touch ".$lockfile);

    $kone = new dzKone();
    $kone->setDbAccess($dbname);
    if ($failed_log != "") $ffp = fopen($failed_log, "a");

    /* Get file name(s) */
    $names = array();
    if (filetype($source) == "dir") {
        $dir = new soDir($folder, "file", "c");
        $fcount = $dir->count();
        for ($f = 0; $f < $fcount; $f++) {
            $names[] = $dir->getValue($f, "n");
        }; // for
    } else {
        $names[] = $source;
    };

    /* Loop files */
    $fcount = count($name);
    for ($f = 0; $f < $fcount; $f++) {
        $name = $names[$f];
        if (($postfix == "") ||
            (strtoupper(substr($name, strlen($name) -4)) ==
            strtoupper($postfix))) {
            $error = 0;
            $streams = new soInputStream("file://$folder/$name");

            if (substr($streams->Data, strlen($streams->Data)
                -strlen(DZIMPEXP_EOF)) != DZIMPEXP_EOF) {
                /* Any file missing the EOF mark should stop the import as it
                 * indicates the write is still in progress and there for is a
                 * good reason to wait a while to let it finish. */
                unlink($lockfile);
                EXIT; // <-- EXIT !!!
            };
            $streams->Data = substr($streams->Data, 0, strlen($streams->Data)
                                    -strlen(DZIMPEXP_EOF));

            $streams = soExplode($streams->Data, $linefeed.$linefeed);

            $set_no = 0;
            foreach ($streams as $stream) {
                $data = soExplode($stream, $linefeed);
                $set = new soSet();
                $set->joinKeyValueArray($data);
                $url = $set->toString(1, "&", "=");

                if ($echo) echo "$name/".++$set_no."\n";

                if (!$kone->_requestStr($url, "&")) {
                    if ($failed_log != "") fwrite($ffp, $stream);
                }; // if error
            }; // foreach

            unlink("$folder/$name");
        }; // if postfix matches

    }; // for $f

    if ($failed_log != "") fclose($ffp);
    unlink($lockfile);

}; // dzImport

?>
